<?php
/**
 * 邮件
 *
 * @author haifeng.wu
 */

class Application_Model_Mail extends Application_Model_Common
{
    /**
     * 状态，返回0为成功，其他为错误
     *
     * @var int
     */
    protected $_status = NULL;

    /**
     * 错误信息
     *
     * @var int
     */
    protected $_info = NULL;
    

    /**
     * 构造函数
     *
     * @return void
     */
    public function __construct()
    {
        parent::__construct();
    }

    /**
     * 发送邮件
     *
     * @param string $address
     * @param string $subject
     * @param string $content
     * @return void
     */
    public function send($address, $subject, $content)
    {
        try{
            $this->_configs = $this->_configModel->getConfigs();
            $config = array('auth' => 'login',
                    'username' => $this->_configs['project']['mailSmtpAccount'],
                    'password' => $this->_configs['project']['mailSmtpPassword'],
                    'port'     => $this->_configs['project']['mailSmtpPort']
            );
            $transport = new Zend_Mail_Transport_Smtp($this->_configs['project']['mailSmtpHost'], $config);
            $mail = new Zend_Mail("UTF-8");
            $mail->setBodyHtml($content);
            $mail->setFrom($this->_configs['project']['mailSmtpAccount'], "{$this->_configs['project']['mailTitleName']}");
            $mail->addTo($address, $address);
            $mail->setSubject($subject);
            $mail->send($transport);

            $expectErrorLog = $transport->getConnection()->getExpectErrorLog();
            if (!empty($expectErrorLog)) {
                $this->_status = 1;
                $this->_info = implode('||', $expectErrorLog);
            } else {
                $this->_status = 0;
                $this->_info = '发送成功';
            }
        } catch(Exception $e) {
            $this->_status = 1;
            $this->_info = $e->getMessage();
        }
    }
    /**
     * 发送邮件到多个地址
     *
     * @param array $addresses
     * @param string $subject
     * @param string $content
     * @param string $att
     * @return void
     */
    public function sendMul($addresses, $subject, $content , $att=null)
    {
        try{
             $this->_configs = $this->_configModel->getConfigs();
            $config = array('auth' => 'login',
                    'username' => $this->_configs['project']['mailSmtpAccount'],
                    'password' => $this->_configs['project']['mailSmtpPassword'],
                    'port'     => $this->_configs['project']['mailSmtpPort']
            );
            $transport = new Zend_Mail_Transport_Smtp($this->_configs['project']['mailSmtpHost'], $config);
            $mail = new Zend_Mail("UTF-8");
            $mail->setBodyHtml($content);
            if ($att) {
            	 $mail->createAttachment(file_get_contents($att), 'application/vnd.ms-excel', Zend_Mime::DISPOSITION_INLINE  ,Zend_Mime::ENCODING_BASE64 , 'report.xls');
            }
            $mail->setFrom($this->_configs['project']['mailSmtpAccount'], "{$this->_configs['project']['mailTitleName']}");
            foreach($addresses as $address) {
                $mail->addTo($address['email'], $address['name']);
            }
            $mail->setSubject($subject);
            $mail->send($transport);

            $expectErrorLog = $transport->getConnection()->getExpectErrorLog();
            if (!empty($expectErrorLog)) {
                $this->_status = 1;
                $this->_info = implode('||', $expectErrorLog);
            } else {
                $this->_status = 0;
                $this->_info = '发送成功';
            }
        } catch(Exception $e) {
            $this->_status = 1;
            $this->_info = $e->getMessage();
        }
    }
    
    /**
     * 发送邮件到多个地址
     *
     * @param string $mainMail 发送地址
     * @param array $addresses
     * @param string $subject
     * @param string $content
     * @param array $att 附件数组 ,(array('filename'=>'xxx','path'=>''))
     * @return void
     */
    public function sendMulMain($mainMail,$addresses, $subject, $content , $atts=null)
    {
        try{
             $this->_configs = $this->_configModel->getConfigs();
            $config = array('auth' => 'login',
                    'username' => $this->_configs['project']['mailSmtpAccount'],
                    'password' => $this->_configs['project']['mailSmtpPassword'],
                    'port'     => $this->_configs['project']['mailSmtpPort']
            );            
            $transport = new Zend_Mail_Transport_Smtp($this->_configs['project']['mailSmtpHost'], $config);
            $mail = new Zend_Mail("UTF-8");
            $mail->setBodyHtml($content);
            if (is_array($atts) && count($atts)) {
                foreach ($atts as $att) {
                    $myImage = file_get_contents($att['path']);
                    $at = new Zend_Mime_Part($myImage);
                    $at->type        = $att['type'];                    
                    $at->disposition = Zend_Mime::DISPOSITION_INLINE;
                    $at->encoding    = Zend_Mime::ENCODING_BASE64;
                    $at->filename    = $att['filename'];
                    $mail->addAttachment($at);
                }
            }
            $mail->setFrom($this->_configs['project']['mailSmtpAccount'], "{$this->_configs['project']['mailTitleName']}");
            $mail->addTo($mainMail, $mainMail);
            if (is_array($addresses) && $addresses) {
                foreach ($addresses as $address) {
                     $mail->addCc($address['email'], $address['name']);
                }
            }
            $mail->setSubject($subject);
            $mail->send($transport);

            $expectErrorLog = $transport->getConnection()->getExpectErrorLog();
            if (!empty($expectErrorLog)) {
                $this->_status = 1;
                $this->_info = implode('||', $expectErrorLog);
            } else {
                $this->_status = 0;
                $this->_info = '发送成功';
            }
        } catch(Exception $e) {
            $this->_status = 1;
            $this->_info = $e->getMessage();
        }
    }

    /**
     * 获取发送状态
     *
     * @return void
     */
    public function getSendStaus() {
        return $this->_status;
    }

    /**
     * 获取发送信息
     *
     * @return void
     */
    public function getSendInfo() {
        return $this->_info;
    }
    
}